package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.dto.UserBriefDTO;
import com.example.springboot.dto.UserDTO;
import com.example.springboot.entity.Follow;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.FollowMapper;
import com.example.springboot.service.ICreativeService;
import com.example.springboot.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.ISolutionService;
import com.example.springboot.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private IUserService userService;
    @Resource
    private IFollowService followService;
    @Resource
    private ICreativeService creativeService;
    @Resource
    private ISolutionService solutionService;

    @Override
    public List<UserBriefDTO> findFollowee(Integer id) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<Follow> list = followService.list(queryWrapper);//Follow表中的数据
        List<Integer> fouserList = new ArrayList<>();
        for(Follow f : list){
            fouserList.add(f.getFouserId());
        }
        return getResult(fouserList);
    }

    @Override
    public List<UserBriefDTO> findFollower(Integer id) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fouser_id",id);
        List<Follow> list = followService.list(queryWrapper);
        List<Integer> userList = new ArrayList<>();
        for(Follow f : list){
            userList.add(f.getUserId());
        }
        return getResult(userList);
    }

    @Override   //关注用户数
    public Integer getFolloweeCount(Integer id) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        return followService.list(queryWrapper).size();
    }

    @Override   //粉丝数
    public Integer getFollowerCount(Integer id) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fouser_id",id);
        return followService.list(queryWrapper).size();
    }

    @Override   //新增关注关系
    public Result insertFollow(Follow follow) {
        if (judge(follow))
            throw new ServiceException(Constants.CODE_600,"当前关注已存在");
        if(follow.getUserId().equals(follow.getFouserId())){//判断用户是否关注的自己
            return Result.error(Constants.CODE_600,"不能对自己进行此操作！");
        }
        if (save(follow))
            return Result.success("true");
        else
            return Result.error(Constants.CODE_600,"false");
    }

    @Override   //取消关注
    public Result removeFollow(Follow follow) {
        if (follow.getUserId() == null || follow.getFouserId() == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",follow.getUserId());
        queryWrapper.eq("fouser_id",follow.getFouserId());
        Follow find;
        try {
            find = getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (find == null)
            throw new ServiceException(Constants.CODE_600,"当前关注不存在");
        if (remove(queryWrapper))
            return Result.success("true");
        else
            return Result.error(Constants.CODE_600,"false");
    }

    //判断是否有关注关系
    private boolean judge(Follow follow) {
        if (follow.getUserId() == null || follow.getFouserId() == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",follow.getUserId());
        queryWrapper.eq("fouser_id",follow.getFouserId());
        Follow find;
        try {
            find = getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        return find != null;
    }

    private List<UserBriefDTO> getResult(List<Integer> fouserList) {//将Follow表中的数据对应User表中，返回我们想要的User表
        List<UserBriefDTO> result = new ArrayList<>();
        if (fouserList.size() == 0) {
            return result;
        }
        List<User> userList = userService.listByIds(fouserList);
        for(User user : userList){
            UserDTO userDTO = new UserDTO();
            UserBriefDTO userBriefDTO = new UserBriefDTO();
            userDTO.setId(user.getId());
            userDTO.setNickname(user.getNickname());
            userDTO.setProfilepic(user.getProfilepic());

            userBriefDTO.setUserDTO(userDTO);
            userBriefDTO.setIntroduction(user.getIntroduction());
            userBriefDTO.setFollowers(followService.getFollowerCount(user.getId()));
            userBriefDTO.setCreatives(creativeService.getByUser(user.getId()).size());
            userBriefDTO.setSolutions(solutionService.findByUserId(user.getId()).size());
            result.add(userBriefDTO);
        }
        return result;
    }
}
